package com.hengfoxwolf.commonbase.base
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment

abstract class BaseFragment<T : ViewDataBinding> : Fragment() {

    protected lateinit var mBinding : T

    protected lateinit var mContext: Context

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false)
        init(savedInstanceState)
        return mBinding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding.unbind()
    }

    protected fun getTextColor(colorRes: Int): Int {
        return mContext.resources.getColor(colorRes)
    }

    protected fun getDimensionValue(dimension: Int): Int {
        return mContext.resources.getDimensionPixelSize(dimension)
    }

    protected fun getDimensionValue2(dimension: Int): Float {
        return mContext.resources.getDimension(dimension)
    }

    protected fun getTextContent(contentId: Int): String {
        return mContext.resources.getString(contentId)
    }

    abstract fun getLayoutId() : Int

    abstract fun init(savedInstanceState: Bundle?)
}